import csv
import requests
import json


def get_address(address):
    url = 'https://restapi.amap.com/v3/geocode/geo?address=' + address + '&output=JOSON&key=22428dc094d1d27215eafb46dcaaa0e9'
    request = requests.get(url).text
    result = json.loads(request)
    if result['status'] == '0':
        return None
    else:
        my_result = str(result['geocodes'][0]['province'])
        return my_result


def is_address(address, address_gangwei):
    address2 = get_address(address_gangwei)
    if address == address2:
        return True
    else:
        return False


def salary_deal(salary):
    sa_value = salary.strip().split('·')
    if len(sa_value) == 1:
        return salary
    else:
        return sa_value[0]


def is_salary(sa1, sa2):
    part1 = sa1.strip().split('-')
    part2 = sa2.strip().split('-')
    part1[0] = int(part1[0])
    part2[0] = int(part2[0])
    return part1[0] <= part2[0]


def compare_degrees(degree1: str, degree2: str) -> bool:
    degree_order = {"博士": 5, "硕士": 4, "本科": 3, "大专": 2, "无学历": 1}
    # Ensure both degrees are valid and in the degree_order dictionary
    if degree1 not in degree_order or degree2 not in degree_order:
        # raise ValueError("Invalid degree specified. Valid degrees are: 博士, 硕士, 本科, 大专.")
        print("errcv")
    return degree_order[degree1] >= degree_order[degree2]


# n表示返回的条数
def all_job_with_condition(job, a, b, c, n):
    job = job.lower()
    if a == '':
        a_true = False
    if b == '':
        b_true = False
    if c == '':
        c_true = False
    csv_file_path = '../Files/Prepare/招聘信息.csv'
    with open(csv_file_path, newline='', encoding='utf-8') as csvfile:
        reader = csv.reader(csvfile)
        # 如果有标题行，可以先读取并跳过
        header = next(reader)
        id_ = []
        result_values = []
        for row in reader:
            result_dict = {}
            # 访问固定列的数据
            col1_data = row[0]  # ID
            col2_data = row[1]  # 公司名称
            col3_data = row[2]  # 岗位
            col4_data = row[3]  # salary
            col5_data = row[4]  # education
            col7_data = row[6]  # hiring_manager
            col9_data = row[8]  # address
            col10_data = row[9]  # city
            col11_data = row[10]  # link
            col12_data = row[11]  # description

            a1_true = True
            b1_true = True
            job_true = True

            if a == '':
                a_true = a1_true
            else:
                col4_data = salary_deal(col4_data)
                a_true = is_salary(a, col4_data)
                if a_true == False:
                    continue
            if b == '':
                b_true = b1_true
            else:
                b_true = compare_degrees(b, col5_data)
                if b_true == False:
                    continue
            if job == '':
                job_true = True
            else:
                job_true = col3_data.find(job) != -1
                if job_true == False:
                    continue
            if a_true and b_true and len(result_values) != n and job_true:
                id_.append(col1_data)
                result_dict['ID'] = col1_data
                result_dict['公司名称'] = col2_data
                result_dict['岗位'] = col3_data
                result_dict['salary'] = col4_data
                result_dict['education'] = col5_data
                result_dict['hiring_manager'] = col7_data
                result_dict['address'] = col9_data
                result_dict['city'] = col10_data
                result_dict['link'] = col11_data
                result_dict['description'] = col12_data
                result_values.append(result_dict)

    return result_values


# n表示返回的条数
def all_job_without_condition(n):
    csv_file_path = '../Files/Prepare/招聘信息.csv'
    with open(csv_file_path, newline='', encoding='utf-8') as csvfile:
        reader = csv.reader(csvfile)
        # 如果有标题行，可以先读取并跳过
        header = next(reader)
        id_ = []
        result_values = []
        for row in reader:
            result_dict = {}
            # 访问固定列的数据
            col1_data = row[0]  # ID
            col2_data = row[1]  # 公司名称
            col3_data = row[2]  # 岗位
            col4_data = row[3]  # salary
            col5_data = row[4]  # education
            col7_data = row[6]  # hiring_manager
            col9_data = row[8]  # address
            col10_data = row[9]  # city
            col11_data = row[10]  # link
            col12_data = row[11]  # description

            if len(result_values) != n:
                id_.append(col1_data)
                result_dict['ID'] = col1_data
                result_dict['公司名称'] = col2_data
                result_dict['岗位'] = col3_data
                result_dict['salary'] = col4_data
                result_dict['education'] = col5_data
                result_dict['hiring_manager'] = col7_data
                result_dict['address'] = col9_data
                result_dict['city'] = col10_data
                result_dict['link'] = col11_data
                result_dict['description'] = col12_data

                result_values.append(result_dict)
    return result_values


# result = get_information('java', '20-30K', '本科', '福建', 10)
# result = alljob_without_condition(1)
# print(result)

def try1():
    print("这个没问题")
